package alpaca.view.panels;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import alpaca.ctrl.AddUserCtrl;
import alpaca.ctrl.ControllerFactory;

public class UserAddDialog extends JDialog {

	private static final String ADD_USER_TITLE = "Add User";
	private final JPanel contentPanel = new JPanel();
	private JTextField lastNameTextField;
	private JTextField firstNameTtextField;
	private JTextField emailTextField;
	private JTextField passwordTextField;
	private final Action action = new CancelAction();
	private final Action action_1 = new AddAction();

	/**
	 * Create the dialog.
	 */
	public UserAddDialog() {
		setTitle(ADD_USER_TITLE);
		setIconImage(new ImageIcon("img//alpaca.png").getImage());
		setBounds(100, 100, 247, 291);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[][grow]", "[10px][][10px][][10px][][10px][]"));
		{
			JLabel lblNewLabel = new JLabel("Last Name");
			contentPanel.add(lblNewLabel, "cell 0 3,alignx right");
		}
		{
			lastNameTextField = new JTextField();
			contentPanel.add(lastNameTextField, "cell 1 3,growx");
			lastNameTextField.setColumns(10);
		}
		{
			JLabel lblNewLabel_1 = new JLabel("First name");
			contentPanel.add(lblNewLabel_1, "cell 0 1,alignx right");
		}
		{
			firstNameTtextField = new JTextField();
			contentPanel.add(firstNameTtextField, "cell 1 1,growx");
			firstNameTtextField.setColumns(10);
		}
		{
			JLabel lblNewLabel_2 = new JLabel("E-mail");
			contentPanel.add(lblNewLabel_2, "cell 0 5,alignx trailing");
		}
		{
			emailTextField = new JTextField();
			contentPanel.add(emailTextField, "cell 1 5,growx");
			emailTextField.setColumns(10);
		}
		{
			JLabel lblNewLabel_3 = new JLabel("Password");
			contentPanel.add(lblNewLabel_3, "cell 0 7,alignx trailing");
		}
		{
			passwordTextField = new JTextField();
			contentPanel.add(passwordTextField, "cell 1 7,growx");
			passwordTextField.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setAction(action_1);
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setAction(action);
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setVisible(true);
		setAlwaysOnTop(true);
	}

	private class CancelAction extends AbstractAction {
		public CancelAction() {
			putValue(NAME, "Cancel");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	}
	private class AddAction extends AbstractAction {
		public AddAction() {
			putValue(NAME, "Add");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			String firstName = firstNameTtextField.getText();
            String lastName = lastNameTextField.getText();
            String email = emailTextField.getText();
            String password = passwordTextField.getText();
            
            AddUserCtrl ctrl = (AddUserCtrl) ControllerFactory.getController(AddUserCtrl.class);
            ctrl.setUser(firstName, lastName, email, password);
            ctrl.execute();
            dispose();
		}
	}
}
